package cn.ctodb.wxmp.service;

import cn.ctodb.wxmp.domain.MessageTp;
import cn.ctodb.wxmp.repository.MessageTpRepository;
import cn.ctodb.wxmp.repository.search.MessageTpSearchRepository;
import cn.ctodb.wxmp.service.dto.MessageTpDTO;
import cn.ctodb.wxmp.service.mapper.MessageTpMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing MessageTp.
 */
@Service
@Transactional
public class MessageTpService {

    private final Logger log = LoggerFactory.getLogger(MessageTpService.class);
    
    @Inject
    private MessageTpRepository messageTpRepository;

    @Inject
    private MessageTpMapper messageTpMapper;

    @Inject
    private MessageTpSearchRepository messageTpSearchRepository;

    /**
     * Save a messageTp.
     *
     * @param messageTpDTO the entity to save
     * @return the persisted entity
     */
    public MessageTpDTO save(MessageTpDTO messageTpDTO) {
        log.debug("Request to save MessageTp : {}", messageTpDTO);
        MessageTp messageTp = messageTpMapper.messageTpDTOToMessageTp(messageTpDTO);
        messageTp = messageTpRepository.save(messageTp);
        MessageTpDTO result = messageTpMapper.messageTpToMessageTpDTO(messageTp);
        messageTpSearchRepository.save(messageTp);
        return result;
    }

    /**
     *  Get all the messageTps.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public Page<MessageTpDTO> findAll(Pageable pageable) {
        log.debug("Request to get all MessageTps");
        Page<MessageTp> result = messageTpRepository.findAll(pageable);
        return result.map(messageTp -> messageTpMapper.messageTpToMessageTpDTO(messageTp));
    }

    /**
     *  Get one messageTp by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public MessageTpDTO findOne(Long id) {
        log.debug("Request to get MessageTp : {}", id);
        MessageTp messageTp = messageTpRepository.findOne(id);
        MessageTpDTO messageTpDTO = messageTpMapper.messageTpToMessageTpDTO(messageTp);
        return messageTpDTO;
    }

    /**
     *  Delete the  messageTp by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete MessageTp : {}", id);
        messageTpRepository.delete(id);
        messageTpSearchRepository.delete(id);
    }

    /**
     * Search for the messageTp corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<MessageTpDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of MessageTps for query {}", query);
        Page<MessageTp> result = messageTpSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(messageTp -> messageTpMapper.messageTpToMessageTpDTO(messageTp));
    }
}
